//
//  VisitorView.swift
//  MicroBlog
//
//  Created by  NorthCity on 15/11/4.
//  Copyright © 2015年  NorthCity. All rights reserved.
//

import UIKit

class VisitorView: UIView {
//MARK: - 外部访问属性
    
//MARK: - 外部访问方法
//    MARK: 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
         setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
//        使用 sb 加载注释此方法,因为会崩溃
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setUpUI()
    }
 
    func setupInfo(imageName:String?,title:String?){
        
        messageLabel.text = title
        guard let imgName = imageName else {
            startAnim()
            return
        }
        iconView.image = UIImage(named: imgName)
        homeIconView.hidden = true
        sendSubviewToBack(maskIconView)
    }
    
    
//MARK: - 私有属性
    
    
    
//MARK: 懒加载控件
//    使用 imageview 构造函数创建的 imageView 大小默认等于图片大小
///   图标
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
///   遮罩图像
    private lazy var maskIconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
///   小房子
    private lazy var homeIconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
///   消息文字
    private lazy var messageLabel : UILabel = UILabel.init(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
///   注册按钮
     lazy var registerButton : UIButton = UIButton.init(title: "注册", color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
///   登录按钮
     lazy var loginButton : UIButton = UIButton.init(title: "登陆", color:  UIColor.darkGrayColor(), backImageName: "common_button_white_disable")
//MARK: - 私有方法
/// 首页轮转动画
    private func startAnim(){
     let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false
//        添加动画到图层
        iconView.layer.addAnimation(anim, forKey: nil)
    }
}
// MARK: - 分类
extension VisitorView{
///设置界面
    private func setUpUI() {
//       1 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
//       2 设置布局
//         1> 图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute:.CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
//         2> 小房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
//         3> 消息文字
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute:.NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute:.NotAnAttribute, multiplier: 1.0, constant: 36))
//         4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute:.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil,attribute:.NotAnAttribute, multiplier: 1.0, constant: 36))
        
//         5> 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute:.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil,attribute:.NotAnAttribute, multiplier: 1.0, constant: 36))
//         6> 遮罩图像
        /**
        VFL : 可视化格式语言
        
        H 水平方向
        V 垂直方向
        | 边界
        [] 包装控件
        views: 是一个字典 [名字: 控件名] - VFL 字符串中表示控件的字符串
        metrics: 是一个字典 [名字: NSNumber] - VFL 字符串中表示某一个数值
        */
       addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskIconView]))
       addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight":-36], views: ["mask":maskIconView,"regButton":registerButton]))
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
}
